package com.csh.subject.domain.handler.subject;

import com.csh.subject.common.enums.IsDeletedFlagEnum;
import com.csh.subject.common.enums.SubjectInfoTypeEnum;
import com.csh.subject.domain.convert.SubjectBriefConvert;
import com.csh.subject.domain.entity.SubjectInfoBo;
import com.csh.subject.domain.entity.SubjectOptionBo;
import com.csh.subject.infra.basic.entity.SubjectBrief;
import com.csh.subject.infra.basic.service.SubjectBriefService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

/**
 * 判断题目的策略类
 */
@Component
@Slf4j
public class BriefTypeHandler implements SubjectTypeHandler{
    @Resource
    private SubjectBriefService subjectBriefService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.BRIEF;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        if (subjectInfoBo.getSubjectAnswer() != null){
            SubjectBrief subjectBrief = SubjectBriefConvert.INSTANCE.convertBoToBrief(subjectInfoBo);
            subjectBrief.setSubjectId(subjectInfoBo.getId().intValue());
            subjectBrief.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            subjectBriefService.insert(subjectBrief);
        }else {
            log.error("简答题插入失败，参考答案不能为空");
        }
    }

    @Override
    public SubjectOptionBo query(Long subjectId) {
        SubjectBrief subjectBrief = new SubjectBrief();
        subjectBrief.setSubjectId(Integer.valueOf(subjectId.intValue()));
        SubjectBrief result = subjectBriefService.queryByCondition(subjectBrief);
        SubjectOptionBo subjectOptionBo = new SubjectOptionBo();
        subjectOptionBo.setSubjectAnswer(result.getSubjectAnswer());
        return subjectOptionBo;
    }
}
